﻿Imports Crystal.DatabaseManagement.BusinessLayer
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Documents.Excel
Imports System.Threading
Imports Crystal.CLL
Imports Infragistics.Win.UltraWinGrid.ExcelExport
Imports DevExpress.Utils
Imports DevExpress.XtraEditors
Imports System.ComponentModel
Imports Infragistics.Win.UltraWinEditors
Imports Infragistics.Win
Imports Crystal.CustomControls.Controls
Imports System.Configuration

Public Class FundManagement
#Region "declare"
    Private _NullField As String = "--------"
    Private _FundName As String = "Fund Name"
    Private _LastDate As String = "Last Report Date"
    Private _SearchField As String = String.Empty
    Private _SearchData As DataTable = Nothing
    Private _SearchValue As String = String.Empty
    Private _OnLoad As Boolean = False
    Private LstModifiedFundId As New List(Of Integer)
    Delegate Sub GridBind(ByVal Dt As DataTable)
    Delegate Sub ControlVisiblityImportDelegate(ByVal isVisible As Integer)

    Public mintInvestorID As Integer
    Public mintLinkID As Integer
    Public mintProjectID As Integer
    Public mdicFundDetails As Dictionary(Of String, Object)

#End Region

#Region "FormEvents"

    Private Sub FundManagement_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not mintLinkID > 0 Then
            If IsChildFormOpened() = False Then
                e.Cancel = True
            Else
                Me.Dispose()
            End If
        Else
            Me.Dispose()
        End If

    End Sub
    Private Sub FundManagement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadFunctionality()
    End Sub
#End Region

#Region "Method"
    Private Function IsChildFormOpened() As Boolean
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "moAddEditFund" Then
                Application.OpenForms(lmFomrs).BringToFront()
                Return False
            End If
        Next
        Return True
    End Function

    Private Sub LoadFunctionality()
        txtSearchIn.Text = MessageManager.GetMessage("DEFAULTSEARCHTEXT")
        ShowDefaultSelectedColumns()
        DefaultDisableCheckedColumns()
        cmbSearchField.DataSource = LoadSearchField()
        _OnLoad = True
        rdAll.Checked = True
        cmbSearchField.SelectedItem = _FundName
        _OnLoad = False
        grdFunds.ContextMenu = New ContextMenu()
        Me.grdFunds.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
        DisableButtonEditDelete(False)
        DisableRightClickInfTextEditor(txtSearchIn)
        BLLmdlCommon.strConfiguration = CommonCLL.mstrConnectionstring
        If mintLinkID > 0 Then
            pnlButton.Visible = False
            pnlCBContainer.Visible = False
            btnCancel.Visible = True
            btnOK.Visible = True
            cmbSearchField.Enabled = False
        End If
        'btnImport.PerformClick()
    End Sub

    Private Function LoadSearchField() As List(Of String)
        Dim lField As New List(Of String)
        For Each ctrl As Control In CType(pnlCBContainer.Controls(0), Misc.UltraPanelClientArea).Controls
            If ctrl.[GetType]().Equals(GetType(UltraCheckEditor)) Then
                If DirectCast(ctrl, UltraCheckEditor).Checked Then
                    lField.Add(ctrl.Text)
                End If
            End If
        Next
        lField.Add(_NullField)
        For Each ctrl As Control In CType(pnlCBContainer.Controls(0), Misc.UltraPanelClientArea).Controls
            If (ctrl.[GetType]().Equals(GetType(UltraCheckEditor))) Then
                If Not DirectCast(ctrl, UltraCheckEditor).Checked Then
                    lField.Add(ctrl.Text)
                End If
            End If
        Next
        Return lField
    End Function

    Public Sub ShowDefaultSelectedColumns()
        uchkStatus.Checked = True
        uchkFundName.Checked = True
        chkType.Checked = True
        chkStyle.Checked = True
        chkTurnover.Checked = True
        chkCountry.Checked = True
    End Sub

    Public Sub DefaultHiddenColumns()
        If grdFunds IsNot Nothing Then
            If grdFunds.Rows.Count > 0 Then
                grdFunds.DisplayLayout.Bands(0).Columns("FundID").Hidden = True
                If grdFunds.DisplayLayout.Bands(0).Columns.Exists("IsCMCFund") Then
                    grdFunds.DisplayLayout.Bands(0).Columns("IsCMCFund").Hidden = True
                End If
                If grdFunds.DisplayLayout.Bands(0).Columns.Exists("Region") Then
                    grdFunds.DisplayLayout.Bands(0).Columns("Region").Hidden = True
                End If
            End If
        End If
    End Sub

    Public Sub ShowCheckedGridColumns()
        Dim Logmessage As New System.Text.StringBuilder
        Try
            DefaultHiddenColumns()
            For Each ctrl As Control In CType(pnlCBContainer.Controls(0), Misc.UltraPanelClientArea).Controls
                If DirectCast(ctrl, UltraCheckEditor).Checked Then
                    With grdFunds
                        If .DisplayLayout.Bands(0).Columns.Exists(ctrl.Text) Then
                            .DisplayLayout.Bands(0).Columns(ctrl.Text).Hidden = False
                            '.DisplayLayout.Rows.Band.Columns(ctrl.Text).PerformAutoResize()
                        End If
                    End With
                Else
                    With grdFunds
                        If .DisplayLayout.Bands(0).Columns.Exists(ctrl.Text) Then
                            .DisplayLayout.Bands(0).Columns(ctrl.Text).Hidden = True
                        End If
                    End With
                End If

            Next
            If (grdFunds.Rows.Count > 0) Then
                grdFunds.DisplayLayout.Bands(0).Columns("Fund Name").PerformAutoResize(grdFunds.Rows.Count)
            End If
        Catch ex As Exception
            Throw ex
            Logmessage.Append("There is error in GetInvestorByFundID of form fundmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
        End Try
    End Sub

    Private Sub GetTooltip(ByVal control As Control, ByVal tooltip As String)
        ToolTip2.SetToolTip(control, ToolTipManager.GetToolTip(tooltip))
    End Sub

    Public Sub DefaultDisableCheckedColumns()
        uchkFundName.Enabled = False
        uchkFundName.Checked = True
        uchkFundName.ForeColor = Color.White
    End Sub

    Private Sub SetTxtSearchText()
        If cmbSearchField.SelectedValue = "Last Report Date" Then
            txtSearchIn.Text = MessageManager.GetMessage("DEFAULTDATEFORMAT").Trim
        Else
            If txtSearchIn.Text = String.Empty Or txtSearchIn.Text = MessageManager.GetMessage("DEFAULTDATEFORMAT").Trim Then
                txtSearchIn.Text = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim
            End If
        End If
    End Sub

    Private Sub ShowMessageInfo(ByVal message As String)
        MessageBox.Show(message, "Crystal", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Function Validations() As Boolean
        Dim Logmessage As New System.Text.StringBuilder
        Try
            If cmbSearchField.Text.Trim = "" Then
                MessageBox.Show(MessageManager.GetMessage("NOFIELDSELECTED"), mstrMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                cmbSearchField.Focus()
                Return True
            End If
            Return False
        Catch ex As Exception
            Throw ex
            Logmessage.Append("There is error in Validation of form fundmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
        End Try
    End Function

    Private Sub SeachFunds()
        If (Me.IsDisposed) Then
            Exit Sub
        End If
        ' If Validations() Then Exit Sub
        LoadFunds()
    End Sub

    Private Function CreateDT()
        Dim Logmessage As New System.Text.StringBuilder
        Dim clone As DataTable = Nothing
        Try
            clone = New DataTable
            clone.Columns.Add("FundId", GetType(Integer))
            clone.Columns.Add("Status", GetType(String))
            clone.Columns.Add("Fund Id", GetType(String))
            clone.Columns.Add("Fund Name", GetType(String))
            clone.Columns.Add("Type", GetType(String))
            clone.Columns.Add("Style", GetType(String))
            clone.Columns.Add("Turnover", GetType(String))
            clone.Columns.Add("Country", GetType(String))
            clone.Columns.Add("Last Report Date", GetType(System.String))
            clone.Columns.Add("Portfolio Value", GetType(Double))
            clone.Columns.Add("P/E Ratio", GetType(Double))
            clone.Columns.Add("P/B Ratio", GetType(Double))
            clone.Columns.Add("Dividend Yield", GetType(Double))
            clone.Columns.Add("Sales Growth", GetType(Double))
            clone.Columns.Add("Price Momentum", GetType(Double))
            clone.Columns.Add("Relative Strength", GetType(Double))
            clone.Columns.Add("Beta", GetType(Double))
            clone.Columns.Add("Investor Name", GetType(String))
            Return clone
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
                Logmessage.Append("There is error in GetInvestorByFundID of form findmanagement.")
                Logmessage.Append(Environment.NewLine)
                Logmessage.Append("Error Message - ")
                Logmessage.Append(ex.ToString())
                Me.LogErrorToDatabase(Logmessage.ToString)
            End If
            Return Nothing
        Finally
            Logmessage = Nothing
            clone = Nothing
        End Try
    End Function

    Public Sub AddParticularFundInGrid(ByVal FundId As Integer)
        If (Me.IsDisposed) Then Exit Sub
        'Dim AddModifyFund As New cAddModifyFund
        Dim dDtFund As New DataTable
        Dim dDtGrdFunds As DataTable
        Dim Dr() As DataRow
        Dim dv As New DataView
        Dim Drow As DataRow = Nothing
        Dim idr As IDataReader = Nothing
        Dim Logmessage As New System.Text.StringBuilder
        Try
            idr = cAddModifyFund.SearchParticularFund(FundId)
            dDtFund.Load(idr)
            mdlCommon.ChangeDataReaderDataTable(dDtFund)
            If dDtFund.Rows.Count = 0 Then Exit Sub
            dDtGrdFunds = CType(grdFunds.DataSource, DataTable)
            If dDtGrdFunds Is Nothing Then
                dDtGrdFunds = CreateDT()
                For i As Integer = 0 To dDtFund.Rows.Count - 1
                    Drow = dDtGrdFunds.NewRow
                    Drow("FundId") = dDtFund.Rows(i)("FundId")
                    Drow("Status") = dDtFund.Rows(i)("Status")
                    Drow("Fund Id") = dDtFund.Rows(i)("Fund Id")
                    Drow("Fund Name") = dDtFund.Rows(i)("Fund Name")
                    Drow("Type") = dDtFund.Rows(i)("Type")
                    Drow("Style") = dDtFund.Rows(i)("Style")
                    Drow("Turnover") = dDtFund.Rows(i)("Turnover")
                    Drow("Country") = dDtFund.Rows(i)("Country")
                    Drow("Last Report Date") = dDtFund.Rows(i)("Last Report Date")
                    Drow("Portfolio Value") = dDtFund.Rows(i)("Portfolio Value")
                    Drow("P/E Ratio") = dDtFund.Rows(i)("P/E Ratio")
                    Drow("P/B Ratio") = dDtFund.Rows(i)("P/B Ratio")
                    Drow("Dividend Yield") = dDtFund.Rows(i)("Dividend Yield")
                    Drow("Sales Growth") = dDtFund.Rows(i)("Sales Growth")
                    Drow("Price Momentum") = dDtFund.Rows(i)("Price Momentum")
                    Drow("Relative Strength") = dDtFund.Rows(i)("Relative Strength")
                    Drow("Beta") = dDtFund.Rows(i)("Beta")
                    Drow("Investor Name") = dDtFund.Rows(i)("Investor Name")
                    dDtGrdFunds.Rows.Add(Drow)
                Next
                dv = New DataView(dDtGrdFunds)
                dv.Sort = "[Fund Name] asc"
                grdFunds.DataSource = dDtGrdFunds
                grdFunds.DisplayLayout.Bands(0).Columns("Fund Name").PerformAutoResize(grdFunds.Rows.Count)
                DefaultHiddenColumns()
                'LoadFund()
                ' Exit Sub
            Else
                If dDtGrdFunds.Rows.Count = 0 Then
                    If dDtFund IsNot Nothing Then
                        If dDtFund.Rows.Count > 0 Then
                            grdFunds.DataSource = dDtFund
                            DefaultHiddenColumns()
                            ShowCheckedGridColumns()
                            If chkInvestorName.Checked Then
                                HideRows(False)
                            Else
                                HideRows(True)
                            End If
                            Exit Sub
                        End If
                    End If
                End If
                If dDtGrdFunds.Rows.Count <> 0 Then
                    Dr = dDtGrdFunds.Select("fundID=" + FundId.ToString())
                    'Dr = dDtGrdFunds.Select("ClientID=" + ClientID.ToString())
                    For Each row As DataRow In Dr
                        dDtGrdFunds.Rows.Remove(row)
                    Next
                    'If Dr.Length > 0 Then
                    '    For i As Integer = 0 To dDtFund.Rows.Count - 1
                    '        Dr(i)("FundId") = dDtFund.Rows(i)("FundId")
                    '        Dr(i)("Status") = dDtFund.Rows(i)("Status")
                    '        Dr(i)("Fund Id") = dDtFund.Rows(i)("Fund Id")
                    '        Dr(i)("Fund Name") = dDtFund.Rows(i)("Fund Name")
                    '        Dr(i)("Type") = dDtFund.Rows(i)("Type")
                    '        Dr(i)("Style") = dDtFund.Rows(i)("Style")
                    '        Dr(i)("Turnover") = dDtFund.Rows(i)("Turnover")
                    '        Dr(i)("Country") = dDtFund.Rows(i)("Country")
                    '        Dr(i)("Last Report Date") = dDtFund.Rows(i)("Last Report Date")
                    '        Dr(i)("Portfolio Value") = dDtFund.Rows(i)("Portfolio Value")
                    '        Dr(i)("P/E Ratio") = dDtFund.Rows(i)("P/E Ratio")
                    '        Dr(i)("P/B Ratio") = dDtFund.Rows(i)("P/B Ratio")
                    '        Dr(i)("Dividend Yield") = dDtFund.Rows(i)("Dividend Yield")
                    '        Dr(i)("Sales Growth") = dDtFund.Rows(i)("Sales Growth")
                    '        Dr(i)("Price Momentum") = dDtFund.Rows(i)("Price Momentum")
                    '        Dr(i)("Relative Strength") = dDtFund.Rows(i)("Relative Strength")
                    '        Dr(i)("Beta") = dDtFund.Rows(i)("Beta")
                    '        Dr(i)("Investor Name") = dDtFund.Rows(i)("Investor Name")
                    '    Next
                    'Else
                    dDtGrdFunds.Merge(dDtFund, True, MissingSchemaAction.Ignore)
                    grdFunds.DataSource = dDtGrdFunds
                    grdFunds.DisplayLayout.Bands(0).Columns("Fund Name").PerformAutoResize(grdFunds.Rows.Count)
                    If chkInvestorName.Checked Then
                        HideRows(False)
                    Else
                        HideRows(True)
                    End If
                    Exit Sub
                    '                End If

                End If
                dv = New DataView(dDtGrdFunds)
                dv.Sort = "[Fund Name] asc"
                grdFunds.DataSource = dv.ToTable()
                grdFunds.DisplayLayout.Bands(0).Columns("Fund Name").PerformAutoResize(grdFunds.Rows.Count)
            End If
            ShowCheckedGridColumns()
            'HideGridColumns()
            'FormatGrid(grdFunds)
            grdFunds.DisplayLayout.Bands(0).Columns("Fund Name").PerformAutoResize(grdFunds.Rows.Count)

        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
                Logmessage.Append("There is error in GetInvestorByFundID of form findmanagement.")
                Logmessage.Append(Environment.NewLine)
                Logmessage.Append("Error Message - ")
                Logmessage.Append(ex.ToString())
                Me.LogErrorToDatabase(Logmessage.ToString)
            End If
        Finally
            If grdFunds.Rows.Count > 1 Then
                DisableButtonEditDelete(True)
                Dim colRow As UltraGridRow = Me.grdFunds.Rows.OfType(Of UltraGridRow)().ToList().Find(Function(row) row.Cells("FundID").Text.Equals(FundId.ToString(), StringComparison.CurrentCultureIgnoreCase))
                grdFunds.ActiveRow = colRow
            Else
                DisableButtonEditDelete(False)
            End If
            Logmessage = Nothing
            'AddModifyFund = Nothing
            dDtFund = Nothing
            dDtGrdFunds = Nothing
            Dr = Nothing
            lblTotalInvestor.Text = "Total Funds:- " + grdFunds.Rows.Count.ToString()

        End Try
    End Sub

    Private Sub EditFund()
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "AddEditFund" Then
                If CType(Application.OpenForms(lmFomrs), AddEditFund).WindowState = FormWindowState.Minimized Then
                    CType(Application.OpenForms(lmFomrs), AddEditFund).WindowState = FormWindowState.Normal
                Else
                    Application.OpenForms(lmFomrs).BringToFront()
                End If
                If (FocusTheExistingInstanceOfForm(grdFunds.ActiveRow.Cells("fundID").Value.ToString(), "AddModifyFund", CType(Application.OpenForms(lmFomrs), AddEditFund).WindowState)) Then
                    Return
                End If
            End If
        Next

        If grdFunds.ActiveRow Is Nothing Then Exit Sub
        If (CheckRecordExistence("fund", grdFunds.ActiveRow.Cells("fundID").Value.ToString())) Then
            If (grdFunds.Selected.Rows.Count > 1) Then
                MessageBox.Show("Please select one record.", mstrMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If
            If grdFunds.ActiveRow Is Nothing Then Exit Sub
            If (CheckRecordExistence("fund", grdFunds.ActiveRow.Cells("fundID").Value.ToString())) Then
                Dim Count = (From ID In LstModifiedFundId _
                         Where ID = grdFunds.ActiveRow.Cells("fundID").Value _
                         Select ID).Count
                If Count = 0 Then
                    Dim name As String = String.Empty
                    name = grdFunds.ActiveRow.Cells("Fund Name").Value
                    Dim lfrmAddModifyFunds As New AddEditFund
                    LstModifiedFundId.Add(grdFunds.ActiveRow.Cells("fundID").Value)
                    lfrmAddModifyFunds.FundId = grdFunds.ActiveRow.Cells("fundID").Value
                    lfrmAddModifyFunds.Text = "Crystal - Edit " + name
                    'lfrmAddModifyFunds.GetInvestorByFundID(grdFunds.ActiveRow.Cells("fundID").Value)
                    'lfrmAddModifyFunds.btnnodify_overview.Enabled = False
                    lfrmAddModifyFunds.StartPosition = FormStartPosition.CenterScreen
                    AddHandler lfrmAddModifyFunds.AddParticularFundInGrid, AddressOf AddParticularFundInGrid
                    AddHandler lfrmAddModifyFunds.CloseUserWindow, AddressOf CloseUserWindow
                    lfrmAddModifyFunds.Show()
                End If

            Else
                grdFunds.ActiveRow.Delete(False)
                MessageBox.Show("Record you are trying to modify/delete either does not exists or deleted by some other user", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If

    End Sub

    Private Sub CheckBoxChangeSearchField()
        _SearchField = cmbSearchField.SelectedItem
        If txtSearchIn.Text.Trim <> MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim Or txtSearchIn.Text.Trim <> MessageManager.GetMessage("DEFAULTDATEFORMAT").Trim Then
            _SearchValue = txtSearchIn.Text
        End If
        cmbSearchField.DataSource = LoadSearchField()
        cmbSearchField.SelectedItem = _SearchField
        If _SearchValue.Length > 0 Then
            txtSearchIn.Text = _SearchValue
        End If
    End Sub

    Public Sub CloseUserWindow(ByVal FundID As Integer)
        LstModifiedFundId.Remove(FundID)
    End Sub

    Private Sub LoadFunds()
        Dim Logmessage As New System.Text.StringBuilder
        'Dim cFundFormat As New cFundFormat
        Dim Searchin As Integer
        Dim LookFor As String = ""
        Dim Funds As New DataTable
        Dim IDR As IDataReader = Nothing
        Dim Criteria As Short
        Dim waitDialog As New WaitDialogForm()
        Try
            lblMessage.Visible = False
            Application.DoEvents()
            _SearchField = cmbSearchField.Text
            If rdContains.Checked Then
                Criteria = SearchCriteria.Contains
            Else
                Criteria = SearchCriteria.BeginWith
            End If
            'End If
            '*************************************************************
            Select Case cmbSearchField.SelectedValue.ToLower
                Case "fund name"
                    Searchin = FundMenu.FundName
                Case "fund id"
                    Searchin = FundMenu.FundID
                Case "status"
                    Searchin = FundMenu.Status
                Case "type"
                    Searchin = FundMenu.Type
                Case "style"
                    Searchin = FundMenu.Style
                Case "turnover"
                    Searchin = FundMenu.Turnover
                Case "country"
                    Searchin = FundMenu.Country
                Case "last report date"
                    Searchin = FundMenu.LastReportDate
                Case "portfolio value"
                    Searchin = FundMenu.PortfolioValue
                Case "p/e ratio"
                    Searchin = FundMenu.PERatio
                Case "p/b ratio"
                    Searchin = FundMenu.PBRatio
                Case "dividend yield"
                    Searchin = FundMenu.DividendYield
                Case "sales growth"
                    Searchin = FundMenu.SalesGrowth
                Case "price momentum"
                    Searchin = FundMenu.PriceMomentum
                Case "relative strength"
                    Searchin = FundMenu.RelativeStrength
                Case "beta"
                    Searchin = FundMenu.Beta
                Case "investor name"
                    Searchin = FundMenu.InvestorName
            End Select
            LookFor = txtSearchIn.Text.Trim
            If LookFor = String.Empty Then
                If rdAll.Checked = False Then
                    _OnLoad = True
                    rdAll.Checked = True
                    _OnLoad = False
                End If
            Else
                If rdAll.Checked Then
                    LookFor = ""
                    SetTxtSearchText()
                    rdAll.Focus()
                End If
            End If
            If rdAll.Checked And LookFor = String.Empty Then
                Searchin = FundMenu.FundName
            End If
            Application.DoEvents()
            pnlButton.Enabled = False
            ButtonDisable(pnlButton.Controls, True)
            waitDialog.Show()
            waitDialog.ResetText()
            waitDialog.Text = "Please wait.."

            IDR = cAddModifyFund.SearchFundDetail(Searchin, LookFor.Replace("'", "''").Replace("_", "[_]").Replace("%", "[%]"), Criteria)
            Funds.Load(IDR)
            mdlCommon.ChangeDataReaderDataTable(Funds)

            If CommonCLL.CheckEmptyDataTable(Funds) Then
                grdFunds.DataSource = New DataTable
                waitDialog.Close()
                MessageBox.Show(MessageManager.GetMessage("NORECORD"), mstrMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                lblTotalInvestor.Text = "Total funds:- 0"
                DisableButtonEditDelete(False)
            Else
                grdFunds.DataSource = Funds
                waitDialog.Close()
                lblTotalInvestor.Text = "Total funds:- " & Funds.Rows.Count
                If chkInvestorName.Checked Then
                    HideRows(False)
                Else
                    HideRows(True)
                End If
            End If

            If Funds.Rows.Count > 0 Then
                ' _SearchData = Funds
                DefaultDisableCheckedColumns()
                ShowCheckedGridColumns()
                DisableButtonEditDelete(True)
                grdFunds.DisplayLayout.Rows.Band.Columns("Fund Name").PerformAutoResize()
                'grdFunds.DisplayLayout.Rows.Band.Columns("Principal Investor").PerformAutoResize()
            End If
            pnlButton.Enabled = True
            ButtonDisable(pnlButton.Controls, False)
            Me.grdFunds.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in LoadFund of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)

        Finally
            Funds = Nothing
            'cFundFormat = Nothing
            IDR = Nothing
            lblTotalInvestor.Visible = True
            waitDialog.Close()
            Logmessage = Nothing
        End Try
    End Sub
    Private Sub DisableButtonEditDelete(ByVal bool As Boolean)
        btnEdit.Enabled = bool
        btnDelete.Enabled = bool
        btnExport.Enabled = bool
    End Sub
    Private Sub ShowColumnOnChecked(ByVal uce As UltraCheckEditor)
        If grdFunds.Rows.Count > 0 Then
            If Not grdFunds.DisplayLayout.Bands(0).Columns.Exists(uce.Text) Then Exit Sub
            If uce.Checked Then
                grdFunds.DisplayLayout.Bands(0).Columns(uce.Text).Hidden = False
            Else
                grdFunds.DisplayLayout.Bands(0).Columns(uce.Text).Hidden = True
            End If
        End If
    End Sub

    Private Sub CheckSelectedField(ByVal text As String)
        For Each ctrl As Control In CType(pnlCBContainer.Controls(0), Misc.UltraPanelClientArea).Controls
            If DirectCast(ctrl, UltraCheckEditor).Text = text Then
                DirectCast(ctrl, UltraCheckEditor).Checked = True
            End If

        Next
    End Sub
    Public Sub LoadImportedInvestor(ByVal funds As DataTable)
        Dim Logmessage As New System.Text.StringBuilder

        Try
            If (Me.IsDisposed) Then Exit Sub
            Me.Invoke(New GridBind(AddressOf LoadGridAfterImport), funds)
            Me.Invoke(New ControlVisiblityImportDelegate(AddressOf VisibleControl), funds.Rows.Count)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in GetInvestorByFundID of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
        End Try
    End Sub
    Private Sub LoadGridAfterImport(ByVal funds As DataTable)
        LoadImportedFunds(funds)
        ShowCheckedGridColumns()
        txtSearchIn.Text = ""
        If (grdFunds.Rows.Count > 0) Then
            DisableButtonEditDelete(True)
        End If
        'FormatGrid(grdFunds)
    End Sub
    Private Sub VisibleControl(ByVal Val As Integer)
        ' lblTotalInvestor.Text = "Total funds:- " & Val
        If Val > 0 Then
            DisableButtonEditDelete(True)
        End If
        btnExport.Enabled = True
        txtSearchIn.Enabled = True
        cmbSearchField.Enabled = True
        btnAddFund.Enabled = True
        'btnModifyUser.Enabled = True
    End Sub
    Public Sub raiseeventfordelete(ByVal dt As DataTable, ByVal isDeleted As Boolean)
        If (Me.IsDisposed) Then Exit Sub
        Dim dDtGrdFunds As DataTable
        Dim indexRow As Integer
        dDtGrdFunds = CType(grdFunds.DataSource, DataTable)
        For Each row As DataRow In dt.Rows
            If isDeleted Then
                indexRow = FindRowIndex(dDtGrdFunds, "FundID", row(0).ToString())
                dDtGrdFunds.Rows(indexRow).Delete()
            Else
                indexRow = FindRowIndex(dDtGrdFunds, "FundID", row(0).ToString())
                dDtGrdFunds.Rows(indexRow)("Status") = "Delete"
            End If
        Next
        Dim dv As New DataView(dDtGrdFunds)
        dv.Sort = "[Fund Name] asc"
        grdFunds.DataSource = dv.ToTable()
        lblTotalInvestor.Text = "Total Funds:- " + grdFunds.Rows.Count.ToString()
        If grdFunds.Rows.Count > 0 Then
            DisableButtonEditDelete(True)
        Else
            DisableButtonEditDelete(False)
        End If

    End Sub
    Public Sub LoadImportedFunds(ByVal dt As DataTable)
        If (Me.IsDisposed) Then Exit Sub
        Dim dDtGrdFunds As DataTable
        Dim indexRow As Integer = 0
        dDtGrdFunds = CType(grdFunds.DataSource, DataTable)
        If dt.Rows.Count = 0 Then
            Exit Sub
        End If
        'If dDtGrdFunds Is Nothing Then
        '    rdSearchResult.Checked = True
        '    grdFunds.DataSource = dt
        '    grdFunds.DisplayLayout.Rows.Band.Columns("Fund Name").PerformAutoResize()
        'Else
        '    For Each row As DataRow In dt.Rows
        '        indexRow = FindRowIndex(dDtGrdFunds, "FundID", row(2).ToString())
        '        If indexRow > 0 Then
        '            dDtGrdFunds.Rows(indexRow).Delete()
        '        End If
        '        AddParticularFundInGrid(row(2))
        '    Next
        'End If
        rdSearchResult.Checked = True
        grdFunds.DataSource = dt
        grdFunds.DisplayLayout.Rows.Band.Columns("Fund Name").PerformAutoResize()
        lblTotalInvestor.Text = "Total Funds:- " + grdFunds.Rows.Count.ToString()
        If grdFunds.Rows.Count > 0 Then
            lblMessage.Visible = False
        End If
    End Sub
#End Region

#Region "Search Field Events"

    Private Sub cmbSearchField_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbSearchField.SelectedIndexChanged
        If _OnLoad = False Then
            If cmbSearchField.SelectedValue = _NullField Then
                ShowMessageInfo(MessageManager.GetMessage("INVALIDSEARCHFIELD").Trim)
                cmbSearchField.Text = _FundName
                Return
            Else
                CheckSelectedField(cmbSearchField.SelectedItem)
                If txtSearchIn.Text.Trim = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim Or txtSearchIn.Text.Trim = MessageManager.GetMessage("DEFAULTDATEFORMAT").Trim Then
                    ' SetTxtSearchText()
                End If
            End If
        End If
    End Sub
#End Region

#Region "TextBox Events"

    Private Sub txtSearchIn_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearchIn.Enter
        If txtSearchIn.Text.Trim = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim Or txtSearchIn.Text.Trim = MessageManager.GetMessage("DEFAULTDATEFORMAT").Trim Then
            txtSearchIn.Text = String.Empty
        End If
    End Sub

    Private Sub txtSearchIn_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearchIn.KeyDown
        If e.KeyCode = Keys.Enter Then
            If cmbSearchField.SelectedValue = _LastDate Then
                If txtSearchIn.Text.Trim().Length < 3 Then
                    ShowMessageInfo(MessageManager.GetMessage("INVALIDDATESEARCH"))
                    Return
                ElseIf txtSearchIn.Text.Trim.Length = 3 Then
                    If IsNumeric(txtSearchIn.Text) Then
                        ShowMessageInfo(MessageManager.GetMessage("INVALIDDATESEARCH"))
                        Return
                    End If
                End If
            End If
            If txtSearchIn.Text.Length > 0 Then
                rdSearchResult.Checked = True
            End If
            SeachFunds()
        End If
    End Sub

#End Region

#Region "ButtonEvents"
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnAddFund_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddFund.Click
        Dim Logmessage As New System.Text.StringBuilder

        Dim objFund As cAddModifyFund = Nothing
        Try
            If (CType(sender, Control).Name = "btnAddFund") Then
                For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
                    If Application.OpenForms(lmFomrs).Name = "AddEditFund" Then
                        If CType(Application.OpenForms(lmFomrs), AddEditFund).WindowState = FormWindowState.Minimized Then
                            CType(Application.OpenForms(lmFomrs), AddEditFund).WindowState = FormWindowState.Maximized
                        Else
                            Application.OpenForms(lmFomrs).BringToFront()
                        End If

                        If (FocusTheExistingInstanceOfForm(0, "AddEditFund", CType(Application.OpenForms(lmFomrs), AddEditFund).WindowState)) Then
                            Return
                        End If
                    End If
                Next
                Dim lfrmAddModifyInvestor As New AddEditFund
                lfrmAddModifyInvestor.Text = "Crystal - Add Fund"
                lfrmAddModifyInvestor.StartPosition = FormStartPosition.CenterScreen
                AddHandler lfrmAddModifyInvestor.AddParticularFundInGrid, AddressOf AddParticularFundInGrid
                lfrmAddModifyInvestor.txtFundName.Focus()
                lfrmAddModifyInvestor.Show()
                Return
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in GetInvestorByFundID of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
        End Try

    End Sub

    Private Sub btnAddFund_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddFund.MouseHover
        GetTooltip(btnAddFund, "ADDNEWFUNDS")
    End Sub

    Private Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        If grdFunds Is Nothing Then Exit Sub
        If Not grdFunds.Rows.Count > 0 Then Exit Sub
        Dim Logmessage As New System.Text.StringBuilder
        'Dim Dlgsave As New SaveFileDialog
        Dim fileName As String = String.Empty
        Dim dialog As New SaveFileDialog
        Dim showDialog As New DialogResult
        Dim name As String
        Dim ExpToExcel As New UltraGridExcelExporter
        Try
            fileName = "Funds - " + DateTime.Now.ToString()
            fileName = fileName.Replace(":", "_").Replace("/", "_")
            With dialog
                .FileName = fileName
                .InitialDirectory = "C:\\"
                .Filter = "Excel (2007-2010 format)(*.xlsx)|*.xlsx|Excel (97-2003 format)(*.xls)|*.xls"
                .DefaultExt = ".xlsx"
            End With
            showDialog = dialog.ShowDialog()
            If showDialog = DialogResult.OK Then
                'lblPleaseWait.Visible = True
                Dim strExtension As String = System.IO.Path.GetExtension(dialog.FileName)
                name = dialog.FileName
                'dialog = Nothing
                'showDialog = Nothing
                Dim selectedformat As WorkbookFormat

                If strExtension = ".xlsx" Then
                    selectedformat = WorkbookFormat.Excel2007
                Else
                    selectedformat = WorkbookFormat.Excel97To2003
                End If
                pnlCBContainer.Enabled = False
                pnlButton.Enabled = False

                Dim ExcWorkbook As New Workbook(selectedformat, WorkbookPaletteMode.CustomPalette)
                Dim lobjws As Worksheet = ExcWorkbook.Worksheets.Add("Fund")
                If rdSearchResult.Checked Then
                    ExpToExcel.Export(grdFunds, lobjws, 0, 0)
                Else
                    Dim column As UltraWinGrid.UltraGridColumn
                    Dim listOfHiddenColumns As New List(Of UltraWinGrid.UltraGridColumn)
                    For Each column In grdFunds.DisplayLayout.Bands(0).Columns
                        If column.Index > 0 Then
                            If column.Hidden = True Then
                                listOfHiddenColumns.Add(column)
                            End If
                            column.Hidden = False
                        End If
                    Next
                    ExpToExcel.Export(grdFunds, lobjws, 0, 0)
                    If listOfHiddenColumns.Count > 0 Then
                        For Each column In listOfHiddenColumns
                            column.Hidden = True
                        Next
                    End If
                End If
                ExcWorkbook.Save(name)
                lblMessage.Visible = False
                pnlCBContainer.Enabled = True
                pnlButton.Enabled = True
                'lblPleaseWait.Visible = False

                ShowMessageInfo(MessageManager.GetMessage("EXCELFILECREATED"))
            End If

        Catch ex As Exception
            ShowMessageInfo(ex.Message)
            Logmessage.Append("There is error in GetInvestorByFundID of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            'lblPleaseWait.Visible = False

            pnlButton.Enabled = True
            pnlButton.Enabled = True
            fileName = String.Empty
            dialog = Nothing
            showDialog = Nothing
            name = Nothing
            ExpToExcel = Nothing
            'Dlgsave = Nothing
            Logmessage = Nothing
        End Try
    End Sub

    Private Sub btnExport_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.MouseHover
        GetTooltip(btnExport, "EXPORTTOEXCEL")
    End Sub

    Private Sub btnImport_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.MouseHover
        GetTooltip(btnImport, "IMPORTFROMEXCEL")
    End Sub
#End Region

#Region "CheckBoxEvents"

    Private Sub chkBeta_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkBeta.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkBeta)
    End Sub

    Private Sub chkCountry_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCountry.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkCountry)
    End Sub

    Private Sub chkDividendYield_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkDividendYield.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkDividendYield)
    End Sub

    Private Sub chkFundID_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkFundID.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkFundID)
    End Sub

    Private Sub chkLastReportDate_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkLastReportDate.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkLastReportDate)
    End Sub

    Private Sub chkPBRatio_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkPBRatio.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkPBRatio)
    End Sub

    Private Sub chkPERatio_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkPERatio.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkPERatio)
    End Sub

    Private Sub chkPortfolioValue_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkPortfolioValue.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkPortfolioValue)
    End Sub

    Private Sub chkPrice_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkPrice.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkPrice)
    End Sub

    Private Sub chkRelativeStrength_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRelativeStrength.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkRelativeStrength)
    End Sub

    Private Sub chkSalesGrowth_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSalesGrowth.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkSalesGrowth)
    End Sub

    Private Sub chkStyle_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkStyle.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkStyle)
    End Sub

    Private Sub chkTurnover_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkTurnover.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkTurnover)
    End Sub

    Private Sub chkType_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkType.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkType)
    End Sub

    Private Sub chkInvestorName_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkInvestorName.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(chkInvestorName)
        If Not chkInvestorName.Checked Then
            HideRows(True)
        Else
            HideRows(False)
        End If
    End Sub
#End Region

#Region "Events"
    Private Sub rdAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If _OnLoad = False Then
            lblMessage.Visible = False
            If rdAll.Checked Then
                SeachFunds()
                SetTxtSearchText()
                rdAll.Focus()
            Else
                If txtSearchIn.Text = String.Empty Then
                    SetTxtSearchText()
                End If
            End If
        End If
    End Sub

    Private Sub grdFunds_AfterSelectChange(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs) Handles grdFunds.AfterSelectChange
        If (grdFunds.Selected.Rows.Count > 1) Then
            ' btnDelete.Enabled = False
            btnEdit.Enabled = False
            If mintLinkID > 0 Then
                btnOK.Enabled = False
            End If
            'DisableAllButtons(False, New Control() {btnEdit})
        Else
            ' btnDelete.Enabled = True
            btnEdit.Enabled = True
            If mintLinkID > 0 Then
                btnOK.Enabled = True
            End If
            'DisableAllButtons(True, New Control() {btnEdit})
        End If
    End Sub

    Private Sub grdFunds_BeforeEnterEditMode(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdFunds.BeforeEnterEditMode
        grdFunds.ActiveRow.Activation = Activation.ActivateOnly
    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If grdFunds.Rows.Count = 0 Then
            'ShowMessageInfo("Please search the funds")
            Exit Sub
        End If
        If (grdFunds.ActiveRow Is Nothing) Then
            ShowMessageInfo(MessageManager.GetMessage("SELECTTHERECORD"))
            Exit Sub
        Else
            grdFunds.Selected.Rows.Add(grdFunds.ActiveRow)
        End If

        If grdFunds.Selected.Rows().Count = 1 Then
            EditFund()
        Else
            ShowMessageInfo(MessageManager.GetMessage("SELECTROW"))
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim FundID As String = ""
        Dim delete As frmDeleteFund
        Dim temp As DataTable = Nothing
        Dim m_pRow As DataRow = Nothing
        Dim Logmessage As New System.Text.StringBuilder

        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "frmDeleteFund" Then
                If CType(Application.OpenForms(lmFomrs), frmDeleteFund).WindowState = FormWindowState.Minimized Then
                    CType(Application.OpenForms(lmFomrs), frmDeleteFund).WindowState = FormWindowState.Maximized
                End If
                If (FocusTheExistingInstanceOfForm("frmDeleteFund", CType(Application.OpenForms(lmFomrs), frmDeleteFund).WindowState)) Then
                    Return
                End If
            End If
        Next
        Try
            If (grdFunds.ActiveRow Is Nothing) Then
                ShowMessageInfo(MessageManager.GetMessage("SELECTTHERECORD"))
                Exit Sub
            Else
                grdFunds.Selected.Rows.Add(grdFunds.ActiveRow)
            End If
            temp = CType(grdFunds.DataSource, DataTable).Clone
            For Each gridRow As UltraGridRow In grdFunds.Selected.Rows
                m_pRow = temp.NewRow()
                m_pRow.ItemArray = CType(gridRow.ListObject, DataRowView).Row.ItemArray
                temp.Rows.Add(m_pRow)
            Next
            delete = New frmDeleteFund(temp)
            AddHandler delete.deletefund, AddressOf raiseeventfordelete
            delete.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in deletefund of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        Finally
            Logmessage = Nothing
            FundID = Nothing
        End Try
    End Sub

    Private Sub grdFunds_DoubleClickRow(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs) Handles grdFunds.DoubleClickRow
        If mintLinkID > 0 Then
            btnOK.PerformClick()
            Exit Sub
        End If
        EditFund()
    End Sub

    Private Sub btnFactset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFactset.Click
        MessageBox.Show("Under development")
    End Sub

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "ImportFund" Then
                If CType(Application.OpenForms(lmFomrs), ImportFund).WindowState = FormWindowState.Minimized Then
                    CType(Application.OpenForms(lmFomrs), ImportFund).WindowState = FormWindowState.Normal
                End If
                If (FocusTheExistingInstanceOfForm("ImportFund", CType(Application.OpenForms(lmFomrs), ImportFund).WindowState)) Then
                    Return
                End If
            End If
        Next
        Dim lfrmImportInvestors As New ImportFund
        AddHandler lfrmImportInvestors.LoadImportedFund, AddressOf LoadImportedInvestor
        lfrmImportInvestors.ShowDialog()
    End Sub

    Private Sub uchkStatus_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uchkStatus.CheckedChanged
        CheckBoxChangeSearchField()
        ShowColumnOnChecked(uchkStatus)
    End Sub

    Private Sub HideRows(ByVal IsTrue As Boolean)
        If grdFunds Is Nothing Then Exit Sub
        If grdFunds.Rows Is Nothing Then Exit Sub
        If grdFunds.Rows.Count = 0 Then Exit Sub
        Dim row As UltraGridRow
        Dim ChildrowCount As Integer = 0
        For Each row In grdFunds.Rows
            'If TypeOf row Is UltraGridGroupByRow Then
            '    Dim groupByRow As UltraGridGroupByRow = DirectCast(row, UltraGridGroupByRow)
            'End If
            If Not Nothing Is row.ChildBands Then
                ChildrowCount = ChildrowCount + 1
                ' Loop throgh each of the child bands.
                Dim childBand As UltraGridChildBand = Nothing
                For Each childBand In row.ChildBands
                    For i As Integer = 0 To childBand.Rows.Count
                        If i < childBand.Rows.Count - 1 Then
                            If childBand.Rows(i).Cells IsNot Nothing AndAlso childBand.Rows(i + 1).Cells IsNot Nothing Then
                                If childBand.Rows(i).Cells(0).Value.ToString = childBand.Rows(i + 1).Cells(0).Value.ToString Then
                                    childBand.Rows(i + 1).Hidden = IsTrue
                                End If
                            End If
                        End If
                    Next
                    ' Call this method recursivedly for each child rows collection.
                Next
            End If

        Next
        If ChildrowCount = 0 Then
            'For Each row In grdFunds.Rows
            For i As Integer = 0 To grdFunds.Rows.Count
                If i < grdFunds.Rows.Count - 1 Then
                    If grdFunds.Rows(i).Cells(0).Value.ToString = grdFunds.Rows(i + 1).Cells(0).Value.ToString Then
                        grdFunds.Rows(i + 1).Hidden = IsTrue
                    End If
                End If

            Next
            'Next
        End If
    End Sub

#End Region

    Private Sub rdAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdAll.Click
        If rdAll.Checked Then
            SeachFunds()
        End If
    End Sub

    Private Sub rdSearchResult_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdSearchResult.Click
        If txtSearchIn.Text = String.Empty Then
            SetTxtSearchText()
        End If
    End Sub


    Private Sub grdFunds_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdFunds.InitializeLayout
        e.Layout.Override.MergedCellStyle = MergedCellStyle.Always
        ' MergedCellEvaluator property can be used to speficy custom logic for merging cells.
        For i As Integer = 1 To e.Layout.Bands(0).Columns.Count - 1
            e.Layout.Bands(0).Columns(i).MergedCellEvaluator = New CustomMergedCellEvaluators()
        Next
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        'If grdFunds.ActiveCell Is Nothing Then Exit Sub
        Dim lstrResult As String = String.Empty
        Dim lboolInt As Boolean = False
        Dim lintNewLinkID As Integer
        Dim lintFundID As Integer
        Dim lstrFndEntityID As String = String.Empty
        Dim lstrFundName As String = String.Empty
        Dim lstrType As String = String.Empty
        Dim lstrStyle As String = String.Empty
        Dim lstrTurnover As String = String.Empty
        Dim lstrCountry As String = String.Empty
        Dim lstrRegion As String = String.Empty
        Dim lboolIsCMCFund As Boolean
        Dim lstrInvStatusCode As String = String.Empty
        Try
            If grdFunds.ActiveRow IsNot Nothing Then
                lintFundID = grdFunds.ActiveRow.GetCellValue("FundID")
                lstrFndEntityID = grdFunds.ActiveRow.GetCellValue("Fund ID")
                lstrFundName = grdFunds.ActiveRow.GetCellValue("Fund Name")
                lstrType = grdFunds.ActiveRow.GetCellValue("Type")
                lstrStyle = grdFunds.ActiveRow.GetCellValue("Style")
                lstrTurnover = grdFunds.ActiveRow.GetCellValue("Turnover")
                lstrCountry = grdFunds.ActiveRow.GetCellValue("Country")
                lstrRegion = grdFunds.ActiveRow.GetCellValue("Region")
                lboolIsCMCFund = grdFunds.ActiveRow.GetCellValue("IsCMCFund")
                lstrInvStatusCode = grdFunds.ActiveRow.GetCellValue("Status")
            End If
            lstrResult = cAddModifyFund.GetNewLinkID(mintProjectID, mintLinkID, mintInvestorID, lintFundID)
            If lstrResult = DBResult.DE_FUND_LINKID_EXISTS Then
                ShowMessageInfo(MessageManager.GetMessage(DBResult.DE_FUND_LINKID_EXISTS.ToString))
            ElseIf lstrResult = DBResult.DE_SAMEFUND_MASTERDB Then
                ShowMessageInfo(MessageManager.GetMessage(DBResult.DE_SAMEFUND_MASTERDB.ToString))
            ElseIf lstrResult = DBResult.DE_CHNGDFND_EXIST Then
                ShowMessageInfo(MessageManager.GetMessage(DBResult.DE_CHNGDFND_EXIST.ToString))
            Else
                lboolInt = Integer.TryParse(lstrResult, lintNewLinkID)
                If lboolInt Then
                    'mintFundID = grdFunds.ActiveCell.Row.GetCellValue(grdFunds.ActiveCell.Band.Columns("FundID"))
                    'mstrFundName = grdFunds.ActiveCell.Row.GetCellValue(grdFunds.ActiveCell.Band.Columns("Fund Name"))
                    mdicFundDetails = New Dictionary(Of String, Object)
                    mdicFundDetails.Add("FundID", lintFundID)
                    mdicFundDetails.Add("FndEntityID", lstrFndEntityID)
                    mdicFundDetails.Add("FundName", lstrFundName)
                    mdicFundDetails.Add("FundType", lstrType)
                    mdicFundDetails.Add("Style", lstrStyle)
                    mdicFundDetails.Add("Turnover", lstrTurnover)
                    mdicFundDetails.Add("LinkID", lintNewLinkID)
                    mdicFundDetails.Add("Country", lstrCountry)
                    mdicFundDetails.Add("Region", lstrRegion)
                    If lboolIsCMCFund = True Then
                        mdicFundDetails.Add("Origin", "C")
                    Else
                        mdicFundDetails.Add("Origin", "F")
                    End If
                    mdicFundDetails.Add("Status", lstrInvStatusCode)
                    If lstrInvStatusCode.ToLower.Equals("active") Then
                        mdicFundDetails.Add("InvStatusCode", EntityStatus.ACTVE.ToString)
                    ElseIf lstrInvStatusCode.ToLower.Equals("dormant") Then
                        mdicFundDetails.Add("InvStatusCode", EntityStatus.DRMNT.ToString)
                    ElseIf lstrInvStatusCode.ToLower.Equals("extinct") Then
                        mdicFundDetails.Add("InvStatusCode", EntityStatus.EXTNT.ToString)
                    Else
                        mdicFundDetails.Add("InvStatusCode", EntityStatus.DELTE.ToString)
                    End If
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Name, mintProjectID)
        Finally
            lstrResult = Nothing
            lboolInt = Nothing
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class
Class CustomMergedCellEvaluators
    Implements Infragistics.Win.UltraWinGrid.IMergedCellEvaluator

    Function ShouldCellsBeMerged(ByVal row1 As UltraGridRow, ByVal row2 As UltraGridRow, ByVal column As UltraGridColumn) As Boolean Implements IMergedCellEvaluator.ShouldCellsBeMerged
        Dim name1 As String = DirectCast(row1.GetCellValue(column).ToString, String)
        Dim name2 As String = DirectCast(row2.GetCellValue(column).ToString, String)
        If column.Key = "Fund Name" Then
            If name1 = name2 Then
                If row1.Index = 0 Then
                    row1.Appearance.BackColor = Color.White
                    row2.Appearance.BackColor = Color.White
                Else
                    If row1.Appearance.BackColor = Color.White Then
                        row2.Appearance.BackColor = Color.White
                    Else
                        row2.Appearance.BackColor = Color.FromArgb(241, 245, 250)
                    End If
                End If
                'row2.Hidden = True
            Else
                If row1.Appearance.BackColor = Color.White Then
                    row2.Appearance.BackColor = Color.FromArgb(241, 245, 250)
                Else
                    row2.Appearance.BackColor = Color.White
                End If
            End If
            Return name1 = name2
        ElseIf column.Key = "Status" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Fund ID" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Type" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Style" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Turnover" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Country" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Last Report Date" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Portfolio Value" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "P/E Ratio" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        ElseIf column.Key = "P/B Ratio" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Dividend Yield" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Sales Growth" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Price Momentum" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Relative Strength" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Beta" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2
        ElseIf column.Key = "Investor Name" Then
            If row2.Cells(0).Value.ToString <> row1.Cells(0).Value.ToString Then
                Return False
            End If
            Return name1 = name2

        End If

        Return False
    End Function
End Class